package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rongke.commons.JsonResp;
import com.rongke.model.Goods;
import com.rongke.model.IndexItem;
import com.rongke.model.PopGoods;
import com.rongke.service.GoodsService;
import com.rongke.service.IndexItemService;
import com.rongke.service.PopGoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version : Ver 1.0
 * @PopGoodsController
 * @人气推荐Controller
 */
@RestController
@RequestMapping(value = "/api/popGoods")
@Transactional
@CrossOrigin
public class PopGoodsController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private PopGoodsService popGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private IndexItemService indexItemService;

    /**
     * @param popGoods
     * @return 返回值JsonResp
     * @添加人气推荐
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addPopGoods(@RequestBody List<PopGoods> popGoods) {
        log.debug("添加人气推荐");
        popGoodsService.insertBatch(popGoods);
        return JsonResp.ok(popGoods);
    }

    /**
     * @param popGoods
     * @return 返回值JsonResp
     * @修改人气推荐
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updatePopGoods(@RequestBody PopGoods popGoods) {
        log.debug("修改人气推荐");
        popGoodsService.updateById(popGoods);
        return JsonResp.ok(popGoods);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找人气推荐
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectPopGoods(Long id) {
        log.debug("查找人气推荐");
        PopGoods popGoods = popGoodsService.selectById(id);
        return JsonResp.ok(popGoods);
    }

    /**
     * @param popGoods
     * @return 返回值JsonResp
     * @根据条件查找人气推荐
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectPopGoodsByCondition(@RequestBody PopGoods popGoods) {
        log.debug("根据条件查找人气推荐");
        if (popGoods == null)
            return JsonResp.toFail("参数不能为空");
        List<PopGoods> popGoodsList = popGoodsService.selectByCondition(popGoods);
        return JsonResp.ok(popGoodsList);
    }

    /**
     * @param page
     * @return 返回值JsonResp
     * @分页查找人气推荐商品
     */
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    public JsonResp selectPopGoods(Page page, Integer type) {
        log.debug("分页查找人气推荐商品");
        if (type == null) {
            return JsonResp.toFail("参数不能为空");
        }
        Map map = new HashMap();
        Integer startIndex = (page.getCurrent() - 1) * page.getSize();
        map.put("startIndex", startIndex);
        map.put("size", page.getSize());
        map.put("type", type);
        List<PopGoods> goodsList = popGoodsService.selectPopGoodsList(map);
        if (!goodsList.isEmpty()) {
            goodsList.forEach(e -> {
                goodsService.fetchGoodsInventoryStatus(e.getGoods());//判断库存状态
            });
        }
        EntityWrapper<PopGoods> ew = new EntityWrapper<>();
        ew.eq("goods_type", type);
        int total = popGoodsService.selectCount(ew);
        page.setRecords(goodsList);
        page.setTotal(total);
        return JsonResp.ok(page);
    }

    /**
     * 获取某产品线下已发布未分组的商品集合（交给web客户端分页）
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "/getNotGroupedGoodsList", method = RequestMethod.GET)
    public JsonResp getNotGroupedGoodsList(Integer type) {
        log.debug("获取某产品线下未分组的商品集合");
        if (null == type)
            return JsonResp.toFail("参数不能为空");
        EntityWrapper<Goods> ew = new EntityWrapper<>();
        //goods_status 商品状态 1代表只上传未发布 2代表正式发布上架 3 下架
        ew.eq("goods_status", 2);
        //蜜哆哆首页栏目（含 0蜜哆哆 和 2品牌馆 两边商品）
        if (type == 7 || type == 8) {
            ew.in("goods_type", Arrays.asList(0, 2));
        } else {
            //查找商品产品线
            EntityWrapper<IndexItem> e = new EntityWrapper<>();
            e.eq("item_num", type)
                    .setSqlSelect("goods_type");
            //单一产品线中选出
            Integer goodsType = (Integer) indexItemService.selectObj(e);
            ew.eq("goods_type", goodsType);
        }
        List<Goods> goodsList = goodsService.selectList(ew);
        Map paramsMap = new HashMap();
        paramsMap.put("type", type);
        //查询已有的商品
        List<PopGoods> popGoodses = popGoodsService.selectPopGoodsList(paramsMap);
        if (!popGoodses.isEmpty()) {
            for (int i = 0, y = popGoodses.size(); i < y; i++) {
                Goods goods = popGoodses.get(i).getGoods();
                if (goodsList.contains(goods)) {
                    goodsList.remove(goods);
                }
            }
        }

        return JsonResp.ok(goodsList);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @删除人气推荐商品
     */
    @RequestMapping(value = "/deletePopGoods", method = RequestMethod.GET)
    public JsonResp deletePopGoods(Long id) {
        log.debug("删除人气推荐商品");
        popGoodsService.deleteById(id);
        return JsonResp.ok("");
    }

    /**
     * @param id
     * @param isHome
     * @return 返回值JsonResp
     * @设置和取消首页显示
     */
    @RequestMapping(value = "/updIsHome", method = RequestMethod.GET)
    public JsonResp updIsHome(Long id, Integer isHome) {
        log.debug("设置和取消首页显示");
        PopGoods ng = popGoodsService.selectById(id);
        ng.setIsHome(isHome);
        popGoodsService.updateById(ng);
        return JsonResp.ok();
    }
}
